package com.penjin.android.activity.secretary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.util.NetUtils;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.http.HttpService;
import com.penjin.android.service.UserService;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * Created by lxl on 2016/4/9.
 */
public class SecretaryNewsDetailsActivity extends Activity {

    private String id ;

    private Context context = this ;

    private String title ;
    private String content ;
    private String pushTime ;
    private String pushPerson ;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what == 0 ){

                setView() ;

            }

        }
    } ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_activity_zhangliang_details);

        Intent intent = getIntent() ;

        id  = intent.getStringExtra("id") ;

        init();

    }

    private void init(  ){


        HttpService httpService = HttpService.getInstance(this);
        //判断网络是否正常
        if (NetUtils.hasNetwork(this)) {
            try {

                RequestParams requestParams = new RequestParams();
                PenjinUser user =  UserService.getInstance(this).getCurrentUser() ;

                requestParams.put("companyId", user.getCompanyId());
                requestParams.put("userName", user.getPhone());
                requestParams.put("id", id);
                httpService.postRequest(this, HttpConstants.HOST + HttpConstants.CompanyPushDetail, requestParams, new TextHttpResponseHandler() {
                    @Override
                    public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                        if (s == null) {
                            Toast.makeText(context, "服务器内部错误", Toast.LENGTH_SHORT).show();
                        } else {
                            System.out.println(s);
                            Toast.makeText(context, "资源路径出错", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onSuccess(int i, Header[] headers, String s) {
                        try {

                            Log.d("JPush",s) ;

                            JSONObject jo = new JSONObject(s);
                            if (jo.getBoolean("result")) {

                                title = jo.getString("title") ;
                                content = jo.getString("content") ;
                                pushTime = jo.getString("pushTime") ;
                                pushPerson = jo.getString("pushPerson") ;


                            }

                            handler.sendEmptyMessage(0) ;

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "网络不可用...", Toast.LENGTH_SHORT).show();
        }

    }


    private void setView(){

        TextView title = (TextView) findViewById(R.id.zhangliang_detail_title);
        TextView content = (TextView) findViewById(R.id.zhangliang_detail_content);
        TextView time = (TextView) findViewById(R.id.zhangliang_detail_time);
        TextView name = (TextView) findViewById(R.id.zhangliang_detail_name);
        ImageView image = (ImageView) findViewById(R.id.zhangliang_detail_image);


        title.setText(this.title);
        content.setText(this.content);
        time.setText(this.pushTime);
        name.setText(this.pushPerson);

    }

}
